package com.cs.pk.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import com.cs.pk.utils.interfaces.ListModificationListener;

public class CustomList implements List {

    private ArrayList list = new ArrayList();
    private ArrayList listenerList = new ArrayList();
    private String name = null;

    public CustomList(String name) {
	this.name = name;
    }

    public boolean add(Object obj) {
	boolean flag = list.add(obj);
	if (flag) {
	    notifyListener(ListModificationListener.ADDITEM, obj);
	}
	return flag;
    }

    public void add(int i, Object obj) {
	list.add(i, obj);
	notifyListener(ListModificationListener.ADDITEM, obj);
    }

    public boolean addAll(Collection collection) {
	boolean flag = list.addAll(collection);
	if (flag) {
	    for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
		Object object = (Object) iterator.next();
		notifyListener(ListModificationListener.ADDITEM, object);
	    }
	}
	return flag;
    }

    public boolean addAll(int i, Collection collection) {
	boolean flag = list.addAll(i, collection);
	if (flag) {
	    for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
		Object object = (Object) iterator.next();
		notifyListener(ListModificationListener.ADDITEM, object);
	    }
	}
	return flag;
    }

    public void clear() {
	for (Iterator iterator = list.iterator(); iterator.hasNext();) {
	    Object type = (Object) iterator.next();
	    notifyListener(ListModificationListener.REMOVEITEM, type);
	}
	list.clear();
    }

    public boolean contains(Object obj) {
	return list.contains(obj);
    }

    public boolean containsAll(Collection collection) {
	return list.containsAll(collection);
    }

    public Object get(int i) {
	return list.get(i);
    }

    public int indexOf(Object obj) {
	return list.indexOf(obj);
    }

    public boolean isEmpty() {
	return list.isEmpty();
    }

    public Iterator iterator() {
	return list.iterator();
    }

    public int lastIndexOf(Object obj) {
	return list.lastIndexOf(obj);
    }

    public ListIterator listIterator() {
	return list.listIterator();
    }

    public ListIterator listIterator(int i) {
	// TODO Auto-generated method stub
	return list.listIterator(i);
    }

    public boolean remove(Object obj) {
	boolean flag = list.remove(obj);
	if (flag) {
	    notifyListener(ListModificationListener.REMOVEITEM, obj);
	}
	return flag;
    }

    public Object remove(int i) {
	Object obj = list.remove(i);
	notifyListener(ListModificationListener.REMOVEITEM, obj);
	return obj;
    }

    public boolean removeAll(Collection collection) {
	boolean flag = list.removeAll(collection);
	if (flag) {
	    for (Iterator iterator = collection.iterator(); iterator.hasNext();) {
		Object type = (Object) iterator.next();
		notifyListener(ListModificationListener.REMOVEITEM, type);
	    }
	}
	return list.removeAll(collection);
    }

    public boolean retainAll(Collection collection) {
	return list.retainAll(collection);
    }

    public Object set(int i, Object obj) {
	return list.set(i, obj);
    }

    public int size() {
	return list.size();
    }

    public List subList(int i, int j) {
	return list.subList(i, j);
    }

    public Object[] toArray() {
	return list.toArray();
    }

    public Object[] toArray(Object[] aobj) {
	return list.toArray(aobj);
    }

    public void notifyListener(int addOrRemoved, Object event) {
	for (Iterator iterator = listenerList.iterator(); iterator.hasNext();) {
	    ListModificationListener type = (ListModificationListener) iterator
		    .next();
	    type.modified(addOrRemoved, event, name);

	}
    }

    public void registerListModificationListener(
	    ListModificationListener listener) {
	listenerList.add(listener);
    }

    public String getName() {
	return name;
    }

}
